home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / nxyplot1.87 / PlotDelegate.m < prev    next >
Encoding:
Text File  |  1992-07-30  |  9.4 KB  |  349 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. /*
  5.  * Some of this code is essentially copied from the AtYourService example
  6.  * of Henry Krempel.
  7.  */
  8.  
  9. #import "PlotDelegate.h"
  10. #import "ServicesHandler.h"
  11. #import "Plot.h"
  12. #import "FormatHandler.h"
  13. #import "PlotView.h"
  14. #import "ScrollWindow.h"             // added 10-7-91 by pdhowell
  15. #import <appkit/Application.h>
  16. #import <appkit/Listener.h>
  17. #import <appkit/Speaker.h>
  18. #import <appkit/Pasteboard.h>
  19. #import <appkit/Panel.h>
  20. #import <strings.h>
  21. #import <appkit/appkit.h>
  22. #import <appkit/Form.h>
  23. #import <defaults.h>
  24. #import <streams/streams.h>
  25. #import <appkit/nextstd.h>
  26. #import <appkit/Application.h>
  27. #import <sys/types.h>        /* only needed in the iconReleasedAt::: code */
  28. #import <sys/stat.h>        /* ditto */
  29.  
  30. @implementation PlotDelegate
  31.  
  32. - appWillInit:sender
  33. {
  34.   // We used to do this in appDidInit, but ran into trouble when we got
  35.   // the Workspace Manager to automatically run nxyplot on files with a
  36.   // selected extension.  The problem was that the 3 windows which get
  37.   // turned into ScrollWindows by the makeSomeScrollWindows method had not been
  38.   // turned into ScrollWindows when a message was sent to them that only
  39.   // a ScrollView (not an ordinary View) could understand.  The solution seems
  40.   // to be to make them into ScrollWindows very early in the game, in the
  41.   // appWillInit method.
  42.   [plotParam makeSomeScrollWindows];
  43.  
  44.   NXUpdateDefaults();
  45.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  46.     [plotParam colorOn:YES];
  47.     [colorOptionButton setState:1];
  48.   }
  49.   else {
  50.     [plotParam colorOn:NO];
  51.     [colorOptionButton setState:0];
  52.   }
  53.   if (strncmp(NXGetDefaultValue("nxyplot", "colorPrinting"), "YES", 3) == 0) {
  54.     [printColorButton setState:1];
  55.     [accPrintColorButton setState:1];
  56.   }
  57.   else {
  58.     [printColorButton setState:0];
  59.     [accPrintColorButton setState:0];
  60.   }
  61.   if (strncmp(NXGetDefaultValue("nxyplot", "cycleLineStyles"), "YES", 3) == 0) {
  62.     [printLineStyleButton setState:1];
  63.     [accPrintLineStyleButton setState:1];
  64.   }
  65.   else {
  66.     [printLineStyleButton setState:0];
  67.     [accPrintLineStyleButton setState:0];
  68.   }
  69.   if (strncmp(NXGetDefaultValue("nxyplot", "opaqueBackground"), "YES", 3) == 0) {
  70.     [opaqueBackgroundButton setState:0];
  71.   }
  72.   else {
  73.     [opaqueBackgroundButton setState:1];
  74.   }
  75.  
  76.   return self;
  77. }
  78.  
  79. - appDidInit:sender
  80. {
  81.   unsigned int windowNum;
  82.   id           speaker;
  83.  
  84.   [[NXApp appListener] setServicesDelegate:self];
  85.   [servicesHandler serviceSetState];
  86.  
  87.   [canvasWindow setMiniwindowIcon:"nxyplot.tiff"];
  88.   [controlPanel setMiniwindowIcon:"nxyplot.tiff"];
  89.  
  90.   /*
  91.    * Register our app icon window with the workspace, so we can accept
  92.    * icons that get dropped on us.  From the Acceptor example in
  93.    * NextDeveloper/Examples.
  94.    */
  95.   NXConvertWinNumToGlobal([[NXApp appIcon] windowNum], &windowNum);
  96.   speaker = [NXApp appSpeaker];
  97.   [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  98.   [speaker registerWindow:windowNum toPort:[[NXApp appListener] listenPort]];
  99.  
  100.   return self;
  101. }
  102.  
  103. - resetDefaults:sender
  104. {
  105.   if ([colorOptionButton state] == 0) {
  106.     NXWriteDefault("nxyplot", "colorOption", "NO");
  107.     [plotParam colorOn:NO];
  108.   }
  109.   else {
  110.     NXWriteDefault("nxyplot", "colorOption", "YES");
  111.     [plotParam colorOn:YES];
  112.   }
  113.   if ([printColorButton state] == 0) {
  114.     NXWriteDefault("nxyplot", "colorPrinting", "NO");
  115.     [accPrintColorButton setState:0];
  116.   }
  117.   else {
  118.     NXWriteDefault("nxyplot", "colorPrinting", "YES");
  119.     [accPrintColorButton setState:1];
  120.   }
  121.   if ([printLineStyleButton state] == 0) {
  122.     NXWriteDefault("nxyplot", "cycleLineStyles", "NO");
  123.     [accPrintLineStyleButton setState:0];
  124.   }
  125.   else {
  126.     NXWriteDefault("nxyplot", "cycleLineStyles", "YES");
  127.     [accPrintLineStyleButton setState:1];
  128.   }
  129.   if ([opaqueBackgroundButton state] == 0) {
  130.     NXWriteDefault("nxyplot", "opaqueBackground", "YES");
  131.   }
  132.   else {
  133.     NXWriteDefault("nxyplot", "opaqueBackground", "NO");
  134.   }
  135.  
  136.   return self;
  137. }
  138.  
  139.  
  140. /* This method takes data from a pasteboard and sends it to be plotted. */
  141. - plotFromPasteboard:pb userData:(const char *)userData error:(char **)errorMessage
  142. {
  143.   char *data;
  144.   int length;
  145.   const char *const *types;
  146.   int hasAscii, i;
  147.  
  148.   types = [pb types];
  149.   hasAscii = 0;
  150.   for (i = 0; !hasAscii && types[i] ; i++) 
  151.     if (!strcmp(types[i], NXAsciiPboardType)
  152.     || !strcmp(types[i], NXTabularTextPboardType)) hasAscii = 1;
  153.  
  154.   if (hasAscii) {
  155.     [pb readType:NXAsciiPboardType data:&data length:&length];
  156.  
  157.     if (data && length) {
  158.       NXStream *dataStream = NXOpenMemory(data, length, NX_READONLY);
  159.       if ([plotParam readData:dataStream :"pasteboard"] == 0) {
  160.     NXRunAlertPanel("Read", "Couldn't read any data from %s", "OK",
  161.             NULL, NULL, "pasteboard");
  162.       }
  163.       [plotParam plotPrepAndDraw];
  164.     }
  165.     else *errorMessage = "No data in your selection.";
  166.   } 
  167.   else *errorMessage = "No ASCII text found in your selection.";
  168.  
  169.   return self;
  170. }
  171.  
  172. // The next 2 methods suggested by Ralph Zazula.  They are needed to get
  173. // WorkspaceManager automatically to run nxyplot when you double-click on
  174. // files with our registered extension(s).
  175.  
  176. - (BOOL)appAcceptsAnotherFile:sender
  177. {
  178.   return YES;
  179. }
  180.  
  181. - (int)app:sender openFile:(const char *)fullPath type:(const char *)aType
  182. {
  183.   [plotParam openFile:(char *)fullPath :(char *)fullPath];
  184.   return YES;
  185. }
  186.  
  187. - getArgs
  188. {
  189.   int i, formatindex = 0, psindex = 0;
  190.   NXStream *formatStream;
  191.   BOOL quit = NO;
  192.  
  193.   [plotParam makeSomeScrollWindows]; /* must do this here! */
  194.   NXUpdateDefaults();        /* ditto */
  195.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  196.     [plotParam colorOn:YES];
  197.     [colorOptionButton setState:1];
  198.   }
  199.   else {
  200.     [plotParam colorOn:NO];
  201.     [colorOptionButton setState:0];
  202.   }
  203.  
  204.   i = 1;
  205.   while (i<NXArgc) {
  206.     if (NXArgv[i][0] == '-') {
  207.       if (NXArgv[i][1] == 'f') { /* format file */
  208.     formatindex = ++i;
  209.       }
  210.       else if (NXArgv[i][1] == 'o') { /* eps file */
  211.     psindex = ++i;
  212.       }
  213.       else if (NXArgv[i][1] == 'q') { /* quit after plotting */
  214.     i++;
  215.     quit = YES;
  216.       }
  217.       else if (NXArgv[i][1] == 'h') {
  218.     NXLogError("usage: nxyplot file [file...] [-f formatfile] [-o epsfile] [-q] [-h]\n");
  219.     exit(0);
  220.       }
  221.       else
  222.     NXLogError("Unrecognized option %s\n", NXArgv[i++]);
  223.     }
  224.     else {
  225.       if (NXMapFile(NXArgv[i], NX_READONLY) == NULL) {
  226.     NXLogError("Unable to open file %s\n", NXArgv[i]);
  227.       }
  228.       else {
  229.     [plotParam openFile:NXArgv[i] :NXArgv[i]];
  230.       }
  231.     }
  232.     i++;
  233.   }
  234.  
  235.   if (formatindex > 0)
  236.     if ((formatStream = NXMapFile(NXArgv[formatindex], NX_READONLY)) == NULL)
  237.       NXLogError("Unable to open format file %s\n", NXArgv[formatindex]);
  238.     else
  239.       [formatHandler readFormatData:formatStream];
  240.  
  241.   if (psindex > 0)
  242.     [canvas savePSCode:NXArgv[psindex]];
  243.  
  244.   if (quit) {
  245.     NXLogError("nxyplot: quit option selected\n");
  246.     [NXApp free];
  247.     exit(0);
  248.   }
  249.   else
  250.     [plotParam drawPlot:self];
  251.  
  252.   return self;
  253. }
  254.  
  255. /*
  256.  * This is copied from the Listener section of the App Kit documentation.
  257.  * All we do is save the pathname for later use.
  258.  */
  259. - (int)iconEntered:(int)windowNum at:(double)x :(double)y
  260.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
  261.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  262.     pathList:(char *)pathList
  263. {
  264.   /* save the file's path for later use; there may be multiple files */
  265.   /* If there are multiple files they are separated by tabs in pathList */
  266.  
  267.   /* allocate space for the path list and copy the string */
  268.   iconPath = (char *)realloc((void *)iconPath, strlen(pathList)+1);
  269.   strncpy(iconPath, pathList, strlen(pathList)+1);
  270.  
  271.   return 0;
  272. }
  273.    
  274. - (int)iconExitedAt:(double)x :(double)y
  275. {
  276.   return 0;
  277. }
  278.  
  279. /*
  280.  * Try to plot the file(s) that have been dragged onto our icon.
  281.  *
  282.  * This note from the Draw example in NextDeveloper/Examples:
  283.  *  Very important: an NX_DURING handler is required around all the processing
  284.  *  of this method since an uncaught raised error will cause this method not
  285.  *  to return and thus hang the Workspace Manager for a while.
  286.  *
  287.  * We also borrow some code from the ImageText MiniExample that checks
  288.  * if a directory has been dragged in.
  289.  */
  290. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  291. {
  292.   char     *stringPosition, *filename;
  293.   struct stat statData;
  294.   int    numfiles=1;
  295.  
  296.   *flag = 1;
  297.  
  298.  
  299.   NX_DURING
  300.   /*
  301.    * If there are any tabs in the iconPath string, multiple files have
  302.    * been dragged in and so we need to avoid the stat call (which returns
  303.    * with an error if given multiple files).
  304.    */
  305.   stringPosition = iconPath;
  306.   while (stringPosition = index(stringPosition, '\t')) {
  307.     numfiles++;
  308.     stringPosition++;
  309.   }
  310.   if (numfiles==1) {
  311.     if (!strcmp(iconPath, "")) {
  312.       /* an empty string means the user dragged root in; do nothing
  313.        * except setting flag to 0 so workspace will reject this.
  314.        */
  315.       *flag = 0;
  316.     }
  317.     else {
  318.       /* If we can't stat the file or if stat tells us the file is a
  319.        * directory, set flag to 0.
  320.        */
  321.       if ( (stat(iconPath, &statData) == -1) || (statData.st_mode & S_IFDIR) ) {
  322.     *flag = 0;
  323.       }
  324.     }
  325.   }
  326.   if (*flag == 1) {
  327.     filename = iconPath;
  328.     while (filename) {
  329.       /*
  330.        * Get the file names one by one and try plotting them.
  331.        */
  332.       stringPosition = strchr(filename, '\t'); /* returns first tab or NULL */
  333.       if (stringPosition) {
  334.     *stringPosition = '\0';
  335.       }
  336.       [plotParam openFile:filename :filename];
  337.       filename = (stringPosition ? ++stringPosition : NULL);
  338.     }
  339.   }
  340.  
  341.   NX_HANDLER
  342.  
  343.   NX_ENDHANDLER
  344.  
  345.   return 0;
  346. }
  347.  
  348. @end
  349.